package com.hfut.jpoa.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.exception.DaoException;
import com.hfut.jpoa.dao.SysparaDAO;
import com.hfut.jpoa.dto.SysparaDTO;
import com.hfut.jpoa.model.Syspara;
import com.ibatis.sqlmap.client.SqlMapClient;

public class SysparaDAOImpl implements SysparaDAO
{
	private static final Logger LOGGER = LoggerFactory
	        .getLogger(SysparaDAOImpl.class);
	private SqlMapClient sqlMapClient;

	public int deleteByPrimaryKey(String id) throws DaoException
	{
		Syspara key = new Syspara();
		key.setId(id);
		int rows = 0;
		try
		{
			rows = sqlMapClient.delete(
			        "tb_sys_syspara.ibatorgenerated_deleteByPrimaryKey", key);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public void insertSelective(Syspara record) throws DaoException
	{
		try
		{
			sqlMapClient.insert(
			        "tb_sys_syspara.ibatorgenerated_insertSelective", record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Syspara selectByPrimaryKey(String id) throws DaoException
	{
		Syspara key = new Syspara();
		key.setId(id);
		Syspara record = null;
		try
		{
			record = (Syspara) sqlMapClient.queryForObject(
			        "tb_sys_syspara.ibatorgenerated_selectByPrimaryKey", key);
			record = (record == null ? new Syspara() : record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return record;
	}

	public int updateByPrimaryKeySelective(Syspara record) throws DaoException
	{
		int rows = 0;
		try
		{
			rows = sqlMapClient
			        .update("tb_sys_syspara.ibatorgenerated_updateByPrimaryKeySelective",
			                record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public int updateByPrimaryKey(Syspara record) throws DaoException
	{
		int rows = 0;
		try
		{
			rows = sqlMapClient
			        .update("tb_sys_syspara.ibatorgenerated_updateByPrimaryKey",
			                record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	/**
	 * @param 对sqlMapClient进行赋值
	 */
	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	/** {@inheritDoc} */
	public void insert(Syspara record) throws DaoException
	{
		try
		{
			sqlMapClient
			        .insert("tb_sys_syspara.ibatorgenerated_insert", record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public ResultDto<Syspara> findSysparaInfoForPage(SysparaDTO sysparaDTO)
	        throws DaoException
	{

		ResultDto<Syspara> result = new ResultDto<Syspara>();
		try
		{
			List<Syspara> configsList = sqlMapClient.queryForList(
			        "tb_sys_syspara.find_syspara_page", sysparaDTO);
			result.setResult(configsList != null ? configsList
			        : new ArrayList<Syspara>());
			BeanUtils.copyProperties(sysparaDTO.getPagination(),
			        result.getPagination());
			result.getPagination().setTotal(
			        (Integer) sqlMapClient.queryForObject(
			                "tb_sys_syspara.find_syspara_page_count",
			                sysparaDTO));
		} catch (SQLException e)
		{
			LOGGER.error("findSysparaInfoForPage exception", e);
			throw new DaoException("findSysparaInfoForPage exception", e);
		}
		return result;
	}

	public List<Syspara> querySysparaList(Syspara syspara)
	{

		List<Syspara> record = null;
		try
		{
			record = sqlMapClient.queryForList(
			        "tb_sys_syspara.ibatorgenerated_select", syspara);
			record = (record == null ? new ArrayList<Syspara>() : record);
		} catch (SQLException e)
		{
			LOGGER.error("querySysparaList exception", e);
			throw new DaoException("querySysparaList exception", e);
		}
		return record;
	}

	public int count(Syspara record) throws DaoException
	{

		int num = 0;

		try
		{
			num = (Integer) sqlMapClient.queryForObject(
			        "tb_sys_syspara.find_syspara_count", record);
		} catch (SQLException e)
		{
			LOGGER.error("count exception", e);
			throw new DaoException("count exception", e);

		}
		return num;
	}
}